package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RightsMapper;
import com.jt.pojo.Rights;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: taoyi
 * @date: 2021/6/10 14:13
 * @version: 1.0
 * @explain:
 */
@Service
public class RightsServiceImpl implements RightsService {
    @Autowired(required = false)
    private RightsMapper rightsMapper;

    @Override
    public List<Rights> list() {
        return rightsMapper.selectList(null);
    }

    /**
     * 1.查询一级列表菜单,条件 parent_id=0
     * 2.查询一级目录下的二级列表信息,条件 parent_id=1 级ID
     */
    @Override
    public List<Rights> getRightsList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",0);
        List<Rights> list = rightsMapper.selectList(queryWrapper);
        // 遍历 list 集合数据
        for (Rights rights : list){
            queryWrapper.clear();
            // 根据一级ID, 查询子级信息
            queryWrapper.eq("parent_id",rights.getId());
            List<Rights> children = rightsMapper.selectList(queryWrapper);
            // 将对象进行封装
            rights.setChildren(children);
        }
        return list;
    }

    public List<Rights> getRightsList2() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",0);
        List<Rights> list = rightsMapper.selectList(queryWrapper);
        // 遍历 list 集合数据
        for (Rights rights : list){
            QueryWrapper queryWrapper2 = new QueryWrapper();
            // 根据一级ID, 查询子级信息
            queryWrapper2.eq("parent_id",rights.getId());
            List<Rights> children = rightsMapper.selectList(queryWrapper2);
            // 将对象进行封装
            rights.setChildren(children);
        }
        return list;
    }

}
